﻿// this function is fired when the document is in the ready state.
function documentLoad() {
    LoadAccounts();
}

function LoadAccounts() {
    var imgLoading = $("#imgLoading");
    $.ajax(
                {
                    url: accountsLoadUrl,
                    data: null,
                    type: "GET",
                    timeout: 10000,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",  // result data type
                    success: function(result) {
                        if (imgLoading[0] != null) {
                            imgLoading[0].visible = false;
                            imgLoading[0].style.display = "none";
                        }
                        var response = JSON.parse(result.d);
                        if (response.IsSuccess == true) {
                            var result2 = response.Payload;
                            var accountsTable = "<table><tr><td><strong>Accounts</strong></td></tr>";
                            for (i = 0; i < result2.length; i++) {
                                accountsTable += "<tr><td><a href='#' "
                                + "onclick=LoadTransactions('" + result2[i].ID + "')>"
                                //+ "onclick=javascript:alert('test')>"
                                //" + result2[i].ID + "
                                + result2[i].AccountName + 
                                "</a></td></tr>";
                            }
                            accountsTable += "</table>";
                            $("#Accounts")[0].innerHTML = accountsTable;
                        }
                        else {
                            ShowErrorMessage(response);
                        }

                    },
                    error: function(xhr, message, ex) {
                        if (imgLoading[0] != null) {
                            imgLoading[0].visible = false;
                            imgLoading[0].style.display = "none";
                        }
                    }
                }
              );
}

function LoadCategories() {
}

function LoadTransactions(accountId) {
    var imgLoading = $("#imgLoading");
    var input = { "accountId": accountId };
    var transactionDate;
    $.ajax(
                {
                    url: transactionsLoadUrl,
                    data: input,
                    type: "GET",
                    timeout: 10000,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",  // result data type
                    success: function(result) {
                        if (imgLoading[0] != null) {
                            imgLoading[0].visible = false;
                            imgLoading[0].style.display = "none";
                        }
                        var response = JSON.parse(result.d);
                        if (response.IsSuccess == true) {
                            var result2 = response.Payload;
                            var accountsTable = "<table><tr><td colspan='3'><strong>Transactions</strong></td></tr><tr><th>Date</th><th>Description</th><th>Amount</th></tr>";
                            for (i = 0; i < result2.length; i++) {
                                transactionDate = ConvertToJSONDate(result2[i].Date);
                                accountsTable += "<tr><td>" +
                                transactionDate.toLocaleDateString() +
                                "</td><td>" + result2[i].Description + "</td><td>" +
                                "$" + result2[i].Amount + "</td></tr>";
                            }
                            accountsTable += "</table>";
                            $("#Transactions")[0].innerHTML = accountsTable;
                        }
                        else {
                            ShowErrorMessage(response);
                        }
                    },
                    error: function(xhr, message, ex) {
                        if (imgLoading[0] != null) {
                            imgLoading[0].visible = false;
                            imgLoading[0].style.display = "none";
                        }
                    }
                }
              );
}